<script type="text/javascript">
	var asInitVals = new Array();
	$(document).ready(function() {
		var oTable = $('#data').dataTable({
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"aaSorting": [ [0,'asc'] ],
			"sDom": 'T<"H"lfr>t<"F"ip>',
			"aoColumnDefs": [
				{ "sType": "formatted-num", "aTargets": [ 3, 4 ] },
				{  "sWidth": '200px', "aTargets": [ 5 ] }
			],
			"oTableTools": {
			"aButtons": [
				{
					"sExtends": "copy",
					"sButtonText": "Copiar",
					"mColumns": [0, 1, 2, 3, 4, 5, 6]
				},
				{
					"sExtends": "print",
					"sButtonText": "Imprimir"
				},
				{
					"sExtends":    "collection",
					"sButtonText": "Guardar como",
					"aButtons":    [
						{
							"sExtends": "xls",
							"mColumns": [0, 1, 2, 3, 4, 5, 6]
						},
						{
							"sExtends": "pdf",
							"sPdfOrientation": "landscape",
							"mColumns": [0, 1, 2, 3, 4, 5, 6],
							"sPdfMessage": "Lista de ventas"
						}
					]
				}
			]
			},
			"fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {

                var iAdelantoDisplay = 0, iTotalDisplay = 0;

                for ( var i=iStart ; i<iEnd ; i++ )
                {
                    iAdelantoDisplay += aaData[ aiDisplay[i] ][3]==='S/.&nbsp;'? 0:parseFloat(aaData[ aiDisplay[i] ][3].replace('S/.&nbsp;' , ''));
                    iTotalDisplay += aaData[ aiDisplay[i] ][4]==='S/.&nbsp;'? 0:parseFloat(aaData[ aiDisplay[i] ][4].replace('S/.&nbsp;' , ''));
				}

                var nCells = nRow.getElementsByTagName('th');
                nCells[3].innerHTML = 'S/.&nbsp;'+addCommas(iAdelantoDisplay.toFixed(2));
                nCells[4].innerHTML = 'S/.&nbsp;'+addCommas(iTotalDisplay.toFixed(2));
            }
        });

        $("tfoot input").keyup( function () {
        	oTable.fnFilter( this.value, $("tfoot input").index(this) );
    	});

		$("tfoot input").each( function (i) {
        	asInitVals[i] = this.value;
    	} );
     
    	$("tfoot input").focus( function () {
        	if ( this.className == "search_init" )
        	{
            	this.className = "";
            	this.value = "";
        	}
    	});
     
    	$("tfoot input").blur( function (i) {
        	if ( this.value == "" )
        	{
            	this.className = "search_init";
            	this.value = asInitVals[$("tfoot input").index(this)];
        	}
    	});
    });
</script>
<!--<a class="DTTT_button ui-state-default" style="float:right; margin-bottom:7px;">
	<span>Nueva Venta</span>
</a>-->
<table cellpadding="0" cellspacing="0" border="0" class="display" id="data">
    <thead>
        <tr>
			<th>Numero</th>
        	<th>Estado</th>
			<th>Fecha</th>
			<th>Mto. Adelanto</th>
			<th>Mto. Total</th>
			<th>Vendedor</th>
			<th>Tienda</th>
			<th>Acciones</th>
        </tr>
    </thead>
    <tbody>
	<?php foreach ($ventas as $v)  { ?>
		<tr>
			<td style="vertical-align:middle;"><?php echo $v['Venta']['nrocomprobante']; ?></td>				
			<td style="vertical-align:middle;">
			<?php				
				if($v['Venta']['estado']== "1"){
				 echo "<span style='color : #FF0000'>Cancelado</span>"; 
				}else{
				  echo "Pendiente"; 	
				}							 
			?>
			</td>
			<td style="vertical-align:middle;"><?php echo $v['Venta']['fecha']; ?></td>
			<td style="text-align:right; vertical-align:middle;"><?php echo 'S/.&nbsp;'.$v['Venta']['adelanto']; ?></td>
			<td style="text-align:right; vertical-align:middle;"><?php echo 'S/.&nbsp;'.$v['Venta']['total']; ?></td>
			<td>
			<?php 
				foreach ($vendedores as $vend) {
					if($v['Venta']['vendedores_id']==$vend['Vendedore']['id'])
					echo $vend['Vendedore']['nombre']." ".$vend['Vendedore']['apellido']; 
		    	} 
		    ?>				
			</td>
			<td style="vertical-align:middle;">
			<?php
				foreach ($tiendas as $t) {
					if($t['Tienda']['id']==$v['Venta']['tiendas_id'])
						echo $t['Tienda']['nombre']; 
				}
			?>
			</td>
			<td style="text-align:center; vertical-align:middle; height:22px;">
				<a class="crud" href="../Ventas/detalle?id=<?php echo $v['Venta']['id']; ?>"><img title="detalle" width="16px" height="16px" src="../css/images/detalle.png"></a>
			</td>
			
		</tr>
	<?php } ?>
    </tbody>
    <tfoot>
		<tr>
			<th>
				<input type="text" value="Numero" class="search_init">
			</th>
			<th>
				<input type="text" value="Estado" class="search_init">
			</th>
			<th>
				<input type="text" value="Fecha" class="search_init">
			</th>
			<th style="text-align:right;">
				<input type="text" value="Mto adelanto" class="search_init">
			</th>
			<th style="text-align:right;">
				<input type="text" value="Mto total" class="search_init">
			</th>
			<th>
				<input type="text" value="Vendedor" class="search_init">
			</th>
			<th>
				<input type="text" value="Tienda" class="search_init">
			</th>
			<th style="text-align: center;">Acciones</th>
		</tr>
	</tfoot>
</table>